<?php
/**
 * "Flash"定义
 * 
 * 一个Flash变量其实是一次性的SESSION，当被读取一次后，就将它的值设为null
 *
 * @since 1.0
 * @package if
 * @subpackage controller.context
 */
class IFlash {
	private static $instance;
	private $prefix = "_FLASH_";
	
	/**
	 * Enter description here...
	 *
	 * @var ISession
	 */
	private $session;
	
	private function __construct() {
		$this->session = ISession::singleton();
	}
	
	/**
	 * Enter description here...
	 *
	 * @return IFlash
	 */
	static function singleton() {
		if (!self::$instance) {
			self::$instance = new self;
		}
		return self::$instance;
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $name
	 * @param unknown_type $value
	 * @since 1.0
	 */
	function set($name, $value) {
		$this->session->set($this->name($name), $value);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $name
	 * @return unknown
	 * @since 1.0
	 */
	function get($name) {
		$value = $this->session->get($this->name($name));
		$this->remove($name);
		return $value;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 1.0
	 */
	function all() {
		$flashs = array();
		foreach ($this->session->all() as $name=>$value) {
			if (preg_match("/^" . preg_quote($this->prefix, "/") . "(.+)$/", $name, $match)) {
				$flashs[$match[1]] = $value;
			}
		}
		return $flashs;
	}
	
	function remove($name) {
		$this->session->remove($this->name($name));
	}
	
	private function name($name) {
		return $this->prefix . $name;
	}
	
	function contains($name) {
		$all = $this->all();
		return array_key_exists($name, $all);
	}
}


?>